<?php
namespace app\api\controller;

use app\common\model\UserCompany;
use app\api\Base;
use app\common\model\User;
use think\Db;
/**
 * 用户face++图片
 */
class Collect extends Base {
    /**
     * 查看是该用户是否有留底照片 默认只传ident_id
     * @return [type] [description]
     */
    public function collect() 
    {
        $msg = ['code'=>400, 'msg'=>'没有留底','data'=>[]];
        //获取用户是否有留底照片
        $UserCompanyModel = Db::name('user_company');
        $identity_id = $this->request->param('identity_id') ? $this->request->param('identity_id') : 0;
        $userid = $this->request->param('userid') ? $this->request->param('userid') : 0;
        if ($userid==0){
            $userid = getUserIdByIdentityid($identity_id);
        }
        $result = Db::name('userCompany')->where(['id' => $identity_id])->field('photoCollect,vface')->find();
       $msg['code'] = 200;

        if($result){
            if($result['photoCollect']){
                $msg['msg'] = "获取成功";
                $msg['data'] = ['photo'=>isset($result['photoCollect'])?$result['photoCollect']:"",'vface'=>$result['vface']];    
                return json_encode($msg);
            }else{
                $msg['data'] = ['photo'=>isset($result['photoCollect'])?$result['photoCollect']:"",'vface'=>4];     # 没有上传
            }
            
        }
        $msg['data'] = ['photo'=>isset($result['photoCollect'])?$result['photoCollect']:"",'vface'=>4];     # 没有上传
        return json_encode($msg);
    }

    /**
     * 上传留底照片
     * @return [type] [description]
     */
    public function photo_collect()
    {
        $type = str_ireplace(array('img', 'images', 'image', '/'), '', $_FILES['file']['type']);
        $data = ['code'=>400,'msg' => '上传失败图片格式不正确', 'data' => []];
        if (in_array($type, array('pjpeg', 'jpeg', 'jpg', 'gif', 'bmp', 'png'))) {

            $result = \Qiniu::instance()->upload(); //七牛上传

            $address = $result[0]['key'];
            $identity_id = $this->request->param('identity_id') ? $this->request->param('identity_id') : 0;
            $userid = $this->request->param('userid') ? $this->request->param('userid') : 0;
            $status = $this->request->param('status') ? $this->request->param('status') : 0;
            if ($userid==0){
                $userid = getUserIdByIdentityid($identity_id);
            }
            if ($address != '') {
                unlink($img_path);
                $path = config("qiniu.QINIU_DOMAIN") . $address;
                $model = Db::name('userCompany');
                $ret = $model->where(['id'=>$identity_id])->update(['photoCollect' => $path, 'vface' => 3]);
                if($ret){
                    $data = ['code'=>200,'msg' => '上传成功', 'data' => ['path'=>$path]];    
                    return json_encode($data);
                }
            } else {

                $data = ['code'=>400,'msg' => '上传失败', 'data' => []];
            }
            return json_encode($data);
        } else {
            $data = ['code'=>400,'msg' => '图片上传类型错误', 'data' => []];
            return json_encode($data);
        }
        return json_encode($data);
    }

}
